package app.consumer;

import biz.paluch.spinach.DisqueClient;
import biz.paluch.spinach.DisqueURI;
import biz.paluch.spinach.api.sync.DisqueCommands;
import com.github.xetorthio.jedisque.Jedisque;
import com.github.xetorthio.jedisque.Job;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import redis.clients.jedis.exceptions.JedisConnectionException;

import java.util.List;

/**
 * @author faith.huan 2018-09-02 16:30
 */
@Slf4j
@Service
@Profile("jedisque")
public class JeDisqueConsumer {

    @Autowired
    private Jedisque jedisque;

    @Scheduled(fixedDelay = 100)

    public void consumeWithJedisque() {

      //  try {
//            Jedisque jedisque = new Jedisque(
//                    // 85
//                    new URI("disque://47.105.39.155:50711")//,
//                    //88
//                    //new URI("disque://47.104.89.100:50711"),
//                    // 83
//                    //new URI("disque://47.104.254.218:50711")
//            );
            int i = 0;
            DisqueClient client = DisqueClient.create(DisqueURI.create("47.105.39.155", 50711));
            DisqueCommands commands = client.connect().sync();
            while (true) {
                i++;
                try {
                    System.out.println("jedisque-----------------" + i);

                    List<Job> jobs = jedisque.getJob(100000, 1, "que01");

                    for (Job job : jobs) {
                        String jobId = job.getId();
                        log.info("第{}次处理,jobID:{},body:{}", i, jobId, job.getBody());

                        try {
                            long ackNum = jedisque.ackjob(jobId);
                            log.info("ackNum:{}", ackNum);
                            while (ackNum!=1){
                                Thread.sleep(1);
                            }
                        }catch (Exception e){
                            log.error("ack异常!",e);

                        }

                    }
                } catch (JedisConnectionException e) {
                    log.error("消费异常", e.getMessage());
                } catch (Exception e) {
                    log.error("xxxxxxxxxxxxxx", e);
                }
            }
//        } catch (URISyntaxException e) {
//            e.printStackTrace();
//        }
    }
}
